package com.communal.service.impl.cr;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.communal.dao.ICommunalDao;
import com.communal.model.Model;
import com.communal.model.cr.CRRent;
import com.communal.model.tarif.TarifRent;
import com.communal.service.ICRService;
import com.communal.service.impl.AbstractService;
import com.communal.util.AppConstants;
import com.communal.util.AppException;
import com.communal.util.CommunalFilterBean;
import com.communal.util.SortPaginationBean;
import com.communal.web.form.cr.CRRentForm;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Service 
public class RentService extends AbstractService implements ICRService<CRRentForm>{

	private final String LOG_ENTITTY = "rent";
	
	@Autowired
	ICommunalDao communalDao;
	
	@Override
	@Transactional(readOnly=true)
	public List<CRRentForm> getRecords(Model p_model, CommunalFilterBean p_communalFilterBean, SortPaginationBean p_sortPaginationBean) throws AppException {
		checkModel( p_model );
		List<CRRentForm> formList = new ArrayList<CRRentForm>();
		try{
			List<CRRent> records = communalDao.getRecords( p_model, CRRent.class, p_communalFilterBean );
			if(CollectionUtils.isNotEmpty(records)){
				for(CRRent record : records){
					formList.add(new CRRentForm( record, 
							applicationUtils.getMonthYearLabel( record.getMonth(), record.getYear() ) ));
				}		
			}
		}catch (Exception err) {
			log.error(messageSource.getMessage("log.error.load", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));
		}
		return formList;
	}
	
	@Override
	@Transactional(readOnly=false)
	public List<CRRentForm> saveOrUpdateRecord(Object p_data, Model p_model) throws AppException {
		checkModel( p_model );
		List<CRRentForm> forms = null;
		try{
			CRRentForm form = new CRRentForm( p_data );
			form.setModelId(p_model.getId());
			
			if(form.getId().equals(AppConstants.FAKE_ID)){
				form.setId( null );
			}
			TarifRent tarif = (TarifRent) baseDao.getById( TarifRent.class, form.getTarifId() );
			
			forms = new ArrayList<CRRentForm>();
			forms.add(new CRRentForm( 
					(CRRent) communalDao.saveOrUpdate(createRecord( form, p_model, tarif )), 
					applicationUtils.getMonthYearLabel( form.getMonth(), form.getYear() )  ));
		}
		catch (Exception err) {
			log.error(messageSource.getMessage("log.error.save", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));
		}
		return forms;
	}

	@Override
	@Transactional(readOnly=false)
	public void deleteRecords(Object p_data, Model p_model) throws AppException {
		checkModel( p_model );
		try{
			Integer id = Integer.parseInt(p_data.toString());
			CRRent record = (CRRent) communalDao.getById(CRRent.class, id);
			if(record != null){
				record.setEnabled( AppConstants.ENTITY_STATE_DISABLED );
				communalDao.saveOrUpdate( record );
			}	
		}catch (Exception err) {
			log.error(messageSource.getMessage("log.error.delete", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));	
		}
	}

	/**
	 * Creates new rent record and set parameters
	 * @param p_form - rent tarif form; {@link CRRentForm} <br>
	 * @param p_model - model; {@link IModel} <br>
	 * @param p_tarif - tarif; {@link TarifRent}
	 */
	@SuppressWarnings("static-access")
	private CRRent createRecord(CRRentForm p_form, Model p_model, TarifRent p_tarif){
		CRRent record = new CRRent();
		record.setId( p_form.getId() );
		record.setEnabled( p_form.getEnabled() );
		record.setModel( p_model );
		record.setDate( applicationUtils.parseISODateToLong( p_form.getDate() ) );
		record.setMonth( p_form.getMonth() );
		record.setYear( p_form.getYear() );
		record.setTarif( p_tarif );
		record.setSumma( p_tarif.getSumma() );
		return record;
	}
}
